cd "C:\Users\creto\Desktop\chinese_students_2017\Submissions\ReStat\Replication Files 2022"


clear
set more off

cap log close


*load data
	use ./kswxy_data, clear

	drop if citycode==.
	
gen pop02 = pop if year==2002
egen popwt02=min(pop02), by(citycode)	

*gen sample to make summ stats with
	gen sample = 1 if balanced==1 & popwt02!=. 
	egen minsample = min(sample), by(citycode)
	drop sample
	rename minsample sample

*standardize/clean up some variables	
	gen exports = dvalue/10000
	
	gen gdppc_true = gdppc*10
	
	gen stud_ot = stud_1 + stud_2 + stud_7 + stud_8
	gen stud_nonfunded = stud_tot - stud_univ_funded
	gen gdp_true = gdp*10000
	gen ln_gdp_true=ln(gdp_true)
	
	foreach x in as ba ma dr ot stem ss arts bus hi_admrate_p0_25 hi_admrate_p25_50 hi_admrate_p50_75 hi_admrate_p75_100 univ_funded nonfunded {
	gen sh_`x' = stud_`x'/stud_tot
	}
	
	
*calculate summary stats
	foreach var of varlist stud_tot sh_as sh_ba sh_ma sh_dr sh_ot sh_stem sh_ss sh_arts ///
						   sh_hi_admrate_p0_25 sh_hi_admrate_p25_50 sh_hi_admrate_p50_75 sh_hi_admrate_p75_100 ///
						   sh_univ_funded sh_nonfunded ln_dvalue pop ln_gdp_true gdp exports gdppc_true {
						   
			eststo `var'_0: estpost tabstat `var' if year==2000 & sample==1, statistics(mean sd N) columns(statistics) listwise
			distinct citycode if year==2004 & sample==1
			estadd scalar cities=r(ndistinct)

			eststo `var'_1: estpost tabstat `var' if year==2013 & sample==1, statistics(mean sd N) columns(statistics) listwise
			distinct citycode if year==2013 & sample==1
			estadd scalar cities=r(ndistinct)
		}	
	
		
*Table 1 (NOTE: FIRST ESTTAB CANNOT HAVE THE prehead(""). LAST ESTTAB CANNOT HAVE postfoot(""). ALL THE ONES IN BETWEEN MUST HAVE postfoot("")/
	
	*pop gdp exports total students
	esttab pop_0 pop_1 using "./table1.tex", replace unstack booktabs ///
	 main(mean %12.0fc) aux(sd %12.0fc) ///
	nodepvars noobs nonotes nostar mtitle("2000" "2013") prefoot("") postfoot("") collabels(none) ///
	varlabels(pop "Population (in 000s)") 
	
	
	esttab gdp_0 gdp_1 using "./table1.tex", append unstack booktabs ///
	 main(mean %12.0fc) aux(sd %12.0fc) ///
	nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("") postfoot("") collabels(none) ///
	varlabels(gdp "GDP (in 10,000 RMB)")
	
	esttab gdppc_true_0 gdppc_true_1 using "./table1.tex", append unstack booktabs ///
	 main(mean %12.0fc) aux(sd %12.0fc) ///
	nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("") postfoot("") collabels(none) ///
	varlabels(gdppc_true "GDP per capita (in RMB)")
	
	esttab exports_0 exports_1 using "./table1.tex", append unstack booktabs ///
	 main(mean %12.0fc) aux(sd %12.0fc) ///
	nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("") postfoot("") collabels(none) ///
	varlabels(exports "Exports (in 10,000 RMB)")
	
	esttab stud_tot_0 stud_tot_1 using "./table1.tex", append unstack booktabs ///
	 main(mean %12.0fc) aux(sd %12.0fc) ///
	nodepvars noobs nonotes nostar nonumbers nomtitles prehead("\\") posthead("") prefoot("") postfoot("") collabels(none) ///
	varlabels(stud_tot "Students Entering\\US Higher Ed\\Per 1M City Residents") 
	
	*academic level
	esttab sh_as_0 sh_as_1 using "./table1.tex", unstack  label ///
	 main(mean %12.2fc) aux(sd %12.2fc) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("\textit{\underline{Academic Level:}} \\") posthead("") postfoot("") prefoot("")  ///
	 varlabels(sh_as "Associates") append

	esttab sh_ba_0 sh_ba_1 using "./table1.tex", unstack  label ///
	 main(mean %12.2fc) aux(sd %12.2fc) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") postfoot("") prefoot("")  ///
	 varlabels(sh_ba "Bachelors") append

	esttab sh_ma_0 sh_ma_1 using "./table1.tex", unstack  label ///
	 main(mean %12.2fc) aux(sd %12.2fc) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") postfoot("") prefoot("")  ///
	 varlabels(sh_ma "Masters") append

	esttab sh_dr_0 sh_dr_1 using "./table1.tex", unstack  label ///
	 main(mean %12.2fc) aux(sd %12.2fc) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") postfoot("") prefoot("")  ///
	 varlabels(sh_dr "Doctorate") append
	
	esttab sh_ot_0 sh_ot_1 using "./table1.tex", unstack  label ///
	 main(mean %12.2fc) aux(sd %12.2fc) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") postfoot("") prefoot("")  ///
	 varlabels(sh_ot "Other") append 


	*field of study 
	esttab sh_stem_0 sh_stem_1 using "./table1.tex", unstack  label ///
	 main(mean %12.2fc) aux(sd %12.2fc) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("\textit{\underline{Field of Study:}} \\") posthead("") postfoot("") prefoot("")  ///
	 varlabels(sh_stem STEM) append
		
	esttab sh_ss_0 sh_ss_1 using "./table1.tex", unstack  label ///
	 main(mean %12.2fc) aux(sd %12.2fc) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") postfoot("") prefoot("")  ///
	 varlabels(sh_ss "Social Science") append 
	 
	esttab sh_arts_0 sh_arts_1 using "./table1.tex", unstack ///
	 main(mean %12.2f) aux(sd %12.2f) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("") postfoot("")  ///
	 varlabels(sh_arts "Arts/Humanities") append
	 
	*admission rate
	
	esttab sh_hi_admrate_p0_25_0 sh_hi_admrate_p0_25_1 using "./table1.tex", unstack ///
	 main(mean %12.2f) aux(sd %12.2f)  ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("\textit{\underline{University Admissions Rate:}} \\") posthead("") prefoot("")  postfoot("")  ///
	 varlabels(sh_hi_admrate_p0_25 "Tier 1 - 1st Quartile") append
	
	esttab sh_hi_admrate_p25_50_0 sh_hi_admrate_p25_50_1 using "./table1.tex", unstack ///
	 main(mean %12.2f) aux(sd %12.2f) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("")  postfoot("")  ///
	 varlabels(sh_hi_admrate_p25_50 "Tier 2 - 2nd Quartile") append
	
	esttab sh_hi_admrate_p50_75_0 sh_hi_admrate_p50_75_1 using "./table1.tex", unstack ///
	 main(mean %12.2f) aux(sd %12.2f) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("")  postfoot("")  ///
	 varlabels(sh_hi_admrate_p50_75 "Tier 3 - 3rd Quartile") append
	
	esttab sh_hi_admrate_p75_100_0 sh_hi_admrate_p75_100_1 using "./table1.tex", unstack ///
	 main(mean %12.2f) aux(sd %12.2f) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("")  postfoot("")  ///
	 varlabels(sh_hi_admrate_p75_100 "Tier 4 - 4th Quartile") append
	
	*funding
	esttab sh_univ_funded_0 sh_univ_funded_1 using "./table1.tex", unstack ///
	 main(mean %12.2f) aux(sd %12.2f) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("\textit{\underline{Scholarship Funding:}} \\") posthead("") prefoot("")  postfoot("")  ///
	 varlabels(sh_univ_funded "Received Funding") append
	
	esttab sh_nonfunded_0 sh_nonfunded_1 using "./table1.tex", unstack ///
	 main(mean %12.2f) aux(sd %12.2f) stats(cities, fmt(%12.0f) labels("Number of Cities")) ///
	 nodepvars noobs nonotes nostar nonumbers nomtitles prehead("") posthead("") prefoot("\hline")  collabels(none) ///
	 varlabels(sh_nonfunded "No Funding") append
 	